﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using CLab.Base;
using GandA.Component.Log;
using CLab.Common.Objects;

namespace CLab.Account
{
    public partial class Login : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ////RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            ////Master.TituloPagina = "Login";
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoginUser.FindControl("UserName").Focus();
            }
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            try
            {
                GandA.Component.Security.Permisos security = new GandA.Component.Security.Permisos("BD");
                if (Membership.ValidateUser(LoginUser.UserName, LoginUser.Password))
                {
                    GandA.Component.Security.PermisoModulo m = security.ObtenModulo(LoginUser.UserName);
                    GandA.Component.Security.Perfil p = security.ObtenPerfil(LoginUser.UserName);
                    UsuarioLogin usu = new UsuarioLogin();
                    usu.IdUsuario = p.IdUsuario;
                    usu.IdSession = Session.SessionID;
                    usu.UserName = p.UserName;
                    usu.Perfil = p;
                    usu.Modulo = m;
                    SessionUsuario = usu;
                    Laboratorio lab = new Laboratorio();
                    lab.IdLaboratorio = p.IdLaboratrio;
                    lab.NombreLaboratorio = p.NombreLaboratorio;
                    lab.IdSucursal = p.IdSucursal;
                    lab.Tema = p.Tema;
                    SessionLaboratorio = lab;
                    if (Request.QueryString["ReturnUrl"] != null)
                    {
                        FormsAuthentication.RedirectFromLoginPage(LoginUser.UserName, false);
                    }
                    else
                    {
                        FormsAuthentication.SetAuthCookie(LoginUser.UserName, false);
                    }
                }
                else
                {
                    alert("Usuario o Contraseña incorrectos");
                }
            }
            catch(Exception ex)
            {
                Log.WriteLog(ex, "Error en Login");
            }
        }

        public void alert(string msg)
        {
            if (Page.IsPostBack)
            {
                ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "alert", "alert('" + msg + "');", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, typeof(Page), "alert", "alert('" + msg + "');", true);
            }
        }
    }
}
